package com.tengfei.main.question.service.excel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tengfei.common.excel.ExcelListener;
import com.tengfei.common.excel.ExcelResult;
import com.tengfei.common.exception.ServiceException;
import com.tengfei.common.utils.ValidatorUtils;
import com.tengfei.common.utils.spring.SpringUtils;
import com.tengfei.main.question.domain.Objective;
import com.tengfei.main.question.domain.bo.ObjectiveBo;
import com.tengfei.main.question.domain.export.ObjImportVo;
import com.tengfei.main.question.mapper.ObjectiveMapper;

import java.util.List;

/**
 * @author feige
 * @version v1.0
 * @date 2022-11-07-21:43
 * @description
 */
public class ObjImportListener extends AnalysisEventListener<ObjImportVo> implements ExcelListener<ObjImportVo> {

    private final ObjectiveMapper objectiveMapper;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public ObjImportListener() {
        objectiveMapper = SpringUtils.getBean(ObjectiveMapper.class);
    }

    // 用户导入数据回调
    @Override
    public void invoke(ObjImportVo objImportVo, AnalysisContext analysisContext) {
        ObjectiveBo objectiveBo = null;
        try {
            objectiveBo = BeanUtil.copyProperties(objImportVo, ObjectiveBo.class);
            if (ObjectUtil.isNull(objectiveBo.getObjGrand()) || objectiveBo.getObjGrand() <= 0) {
                objectiveBo.setObjGrand(Math.round((float) (Math.random() * 5) + 1));
            }
            ValidatorUtils.validate(objectiveBo);
            Objective objective = BeanUtil.copyProperties(objectiveBo, Objective.class);
            objectiveMapper.insert(objective);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、客观题 ").append(objectiveBo.getObjContent()).append(" 添加成功");
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、客观题 " + objectiveBo.getObjContent() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

    @Override
    public ExcelResult<ObjImportVo> getExcelResult() {
        return new ExcelResult<ObjImportVo>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<ObjImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
